package com.pz.cloud.framework.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 用于日历的工具类
 */
public final class CalendarUtil {
    /**
     * yyyy-MM-dd
     */
    public final static String FORMAT_ONE = "yyyy-MM-dd"; //格式一

    /**
     * yyyy/MM/dd
     */
    public final static String FORMAT_TWO = "yyyy/MM/dd"; //格式二

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public final static String FORMAT_THREE = "yyyy-MM-dd HH:mm:ss"; //格式三

    /**
     * yyyy/MM/dd HH/mm/ss
     */
    public final static String FORMAT_FOUR = "yyyy/MM/dd HH/mm/ss"; //格式四

    /**
     * yyyyMMddHHmmss
     */
    public final static String FORMAT_FIVE = "yyyyMMddHHmmss"; //格式五

    /**
     * yyyy年MM月dd日
     */
    public final static String FORMAT_SIX = "yyyy年MM月dd日"; //格式六

    /**
     * yyyy-MM
     */
    public final static String FORMAT_SEVEN = "yyyy-MM"; //格式七

    private final static Map<String, SimpleDateFormat> FORMATTERS = new HashMap<>();

    static {
        FORMATTERS.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
        FORMATTERS.put("yyyy/MM/dd", new SimpleDateFormat("yyyy/MM/dd"));
        FORMATTERS.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        FORMATTERS.put("yyyy/MM/dd HH/mm/ss", new SimpleDateFormat("yyyy/MM/dd HH/mm/ss"));
        FORMATTERS.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
        FORMATTERS.put("yyyy年MM月dd日", new SimpleDateFormat("yyyy年MM月dd日"));
        FORMATTERS.put("yyyy-MM", new SimpleDateFormat("yyyy-MM"));
    }

    /**
     * 转化字符串至date对象
     * @param patten 格式
     * @return date对象
     * @throws ParseException 转换异常
     */
    public static Date getCurrentDate(String patten){

        SimpleDateFormat dateFormat = getDateFormat(patten);
        try {
            return dateFormat.parse(dateFormat.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 转化字符串至date对象
     * @param dateStr 日期字符串
     * @param patten 格式
     * @return date对象
     * @throws ParseException 转换异常
     */
    public static Date parse(String dateStr, String patten){
        if (dateStr == null || dateStr.trim().equals("")) {
            return null;
        }

        SimpleDateFormat dateFormat = getDateFormat(patten);
        try {
            return dateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 格式化日期
     * @param date 日期值
     * @param patten 格式
     * @return 格式化日期
     */
    public static String format(Date date, String patten) {
        if (date == null) {
            return null;
        }

        SimpleDateFormat dateFormat = getDateFormat(patten);
        return dateFormat.format(date);
    }

    /**
     * 获取日期格式化的类
     * @param patten 日期格式
     * @return 日期格式化的类
     */
    private synchronized static SimpleDateFormat getDateFormat(String patten) {
        if (FORMATTERS.get(patten) == null) {
            FORMATTERS.put(patten, new SimpleDateFormat(patten));
        }

        return FORMATTERS.get(patten);
    }
    public static Date calendarAdd(Date date,int field,int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field,amount);
        return calendar.getTime();
    }

    /**
     * 获取指定日期对应的星期中的天数
     *
     * @param date : 日期
     * @return 星期中的天数
     */
    public static String getDayOfWeek(Date date) throws Exception {
        if (date == null) {
            throw new Exception("无效的日期值(null)，无法获取对应的星期中的天数");
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            switch (cal.get(Calendar.DAY_OF_WEEK)) {
                case Calendar.MONDAY:
                    return "星期一";
                case Calendar.TUESDAY:
                    return "星期二";
                case Calendar.WEDNESDAY:
                    return "星期三";
                case Calendar.THURSDAY:
                    return "星期四";
                case Calendar.FRIDAY:
                    return "星期五";
                case Calendar.SATURDAY:
                    return "星期六";
                default:
                    return "星期日";
            }
        }
    }

    /**
     * 获取指定日期对应的当月中的第几周
     *
     * @param date : 日期
     * @return 月中的第几周
     */
    public static String getWeekOfMonth(Date date) throws Exception {
        if (date == null) {
            throw new Exception("无效的日期值(null)，无法获取日期对应的当月中的第几周");
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            switch (cal.get(Calendar.WEEK_OF_MONTH)) {
                case 1:
                    return "第一周";
                case 2:
                    return "第二周";
                case 3:
                    return "第三周";
                case 4:
                    return "第四周";
                default:
                    return "第五周";
            }
        }
    }


    /**
     * <p>
     * 说明:Date转String
     * @param date 日期
     * @param format 格式
     * @return
     * </p>
     */
    public static String dateToString(Date date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = "";
        if(!"".equals(date) && date != null){
            s = sdf.format(date);
        }

        return s;
    }

    /**
     * 得到指定年份的所有周末
     */
    public static List<Map<String,Date>> getWeekDayList(int year) {
        List<Map<String,Date>> listDate = new ArrayList<Map<String,Date>>();
        int i = 1;
        Calendar calendar = new GregorianCalendar(year, 0, 1);
        while (calendar.get(Calendar.YEAR) < year + 1) {
            Map<String,Date>map = new HashMap<>();
            calendar.set(Calendar.WEEK_OF_YEAR, i++);
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                map.put("sunday",calendar.getTime());
            }
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            if (calendar.get(Calendar.YEAR) == year) {
                map.put("saturday",calendar.getTime());
            }
            listDate.add(map);
        }
        return listDate;
    }

    /**
     * @return
     * @description 获取当前日期
     */
    public static Date  getToDay(String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date today = null;
        try {
            today = sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return today;
    }


    public static Date getYesterday(){
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH,-1);
        return c.getTime();
    }

    public static Date addDay(Date date,int day){
        Calendar c =Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH,day);
        return c.getTime();
    }

    /**
     * 判断是否是周五
     * @param dateStr
     * @return
     */
    public static boolean isFriday(String dateStr){
        Date date =parse(dateStr,FORMAT_ONE);
        Calendar cal =Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK)==Calendar.FRIDAY;
    }

    /**
     * 判断是否是周六
     * @param dateStr
     * @return
     */
    public static boolean isSaturday(String dateStr){
        Date date =parse(dateStr,FORMAT_ONE);
        Calendar cal =Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY;
    }

    /**
     * 判断是否是周日
     * @param dateStr
     * @return
     */
    public static boolean isSunday(String dateStr){
        Date date =parse(dateStr,FORMAT_ONE);
        Calendar cal =Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY;
    }

    /**
     * 工具类无需实例化
     */
    private CalendarUtil() {

    }

    public static void main(String[] args) {

//        System.out.println(dateToString(getYesterday(),FORMAT_ONE)+" 00:00:00");
//        System.out.println(dateToString(addDay(new Date(),2),FORMAT_THREE));

        System.out.println(isSaturday("2018-05-26"));
        System.out.println(isSunday("2018-05-26"));
    }
}
